package com.simple.admin.controller.system;

import com.simple.framework.constant.HttpStatus;
import com.simple.framework.core.controller.BaseController;
import com.simple.framework.core.domain.R;
import com.simple.framework.core.domain.dto.ExportExcelDto;
import com.simple.framework.core.domain.vo.PagingVo;
import com.simple.framework.utils.EasyExcelUtil;
import com.simple.system.annotation.SystemLog;
import com.simple.system.domain.dto.AddSysUserDto;
import com.simple.system.domain.dto.ListSysUserDto;
import com.simple.system.domain.dto.UpdateByLoginPasswordDto;
import com.simple.system.domain.dto.UpdateByLoginUserDto;
import com.simple.system.domain.vo.SysListUserSelectVo;
import com.simple.system.domain.vo.SysListUserVo;
import com.simple.system.domain.vo.SysUserDetailsVo;
import com.simple.system.enums.BusinessType;
import com.simple.system.service.SysUserService;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * @Description 用户接口-控制器
 * @Author xiaoke
 * @Date 2024/8/10 18:00
 * @Version 1.0
 */
@RestController
@RequestMapping("/sys_user")
public class SysUserController extends BaseController {

    private final SysUserService sysUserService;

    public SysUserController(SysUserService sysUserService) {
        this.sysUserService = sysUserService;
    }

    /**
     * 分页获取用户列表
     *
     * @param dto 请求参数
     * @return PagingVo<SysListUserVo>
     */
//    @PreAuthorize("hasAuthority('system:user:query')")
    @GetMapping
    public R<PagingVo<SysListUserVo>> list(ListSysUserDto dto) {
        PagingVo<SysListUserVo> list = sysUserService.list(dto);
        return success(list);
    }

    /**
     * 获取用户下拉列表
     *
     * @return List<SysList
     */
    @GetMapping("select")
    public R<List<SysListUserSelectVo>> listSelect() {
        return success(sysUserService.listSelect());
    }

    /**
     * 根据手机号码获取用户信息
     *
     * @param cellphone 手机号码
     * @return SysUser
     */
    @GetMapping("/cellphone/{cellphone}")
    public R<SysUserDetailsVo> findByCellphoneUser(@PathVariable("cellphone") String cellphone) {
        SysUserDetailsVo user = sysUserService.getByCellphone(cellphone);
        return success(user);
    }

    /**
     * 修改当前用户信息
     *
     * @param dto 请求参数
     * @return Void
     */
    @SystemLog(title = "用户修改个人资料", businessType = BusinessType.UPDATE)
    @PutMapping("/update/userInfo")
    public R<Void> updateByLoginUser(@RequestBody @Validated UpdateByLoginUserDto dto) {
        sysUserService.updateByLoginUser(dto);
        return success();
    }

    @SystemLog(title = "用户重置密码", businessType = BusinessType.UPDATE)
    @PutMapping("/update/password")
    public R<Void> updateByLoginPassword(@RequestBody @Validated UpdateByLoginPasswordDto dto) {
        sysUserService.updateByLoginPassword(dto);
        return success();
    }


    /**
     * 根据用户Id获取用户权限
     *
     * @param userId 用户Id
     * @return List<String>
     */
    @GetMapping("perms/{id}")
    public R<List<String>> getUserPerms(@PathVariable("id") Long userId) {
        List<String> perms = sysUserService.getUserPerms(userId);
        return success(perms);
    }

    /**
     * 根据用户Id获取用户详情
     *
     * @param id 用户Id
     * @return SysUserDetailsVo
     */
    @GetMapping("{id}")
    public R<SysUserDetailsVo> getUserDetails(@PathVariable("id") Long id) {
        SysUserDetailsVo userDetails = sysUserService.getUserDetails(id);
        return success(userDetails);
    }

    /**
     * 新增用户
     *
     * @param dto 请求参数
     * @return 用户Id
     */
    @SystemLog(title = "新增用户", businessType = BusinessType.INSERT)
    @PostMapping
    public R<Long> create(@RequestBody @Validated AddSysUserDto dto) {
        Long userId = sysUserService.create(dto);
        return success(HttpStatus.CREATED, userId);
    }

    /**
     * 修改用户
     *
     * @param id  用户Id
     * @param dto 请求参数
     * @return Void
     */
    @SystemLog(title = "修改用户", businessType = BusinessType.UPDATE)
    @PutMapping("{id}")
    public R<Void> update(@PathVariable("id") Long id, @RequestBody @Validated AddSysUserDto dto) {
        sysUserService.update(id, dto);
        return success();
    }

    /**
     * 删除用户
     *
     * @param id 用户Id
     * @return Void
     */
    @SystemLog(title = "删除用户", businessType = BusinessType.DELETE)
    @DeleteMapping("{id}")
    public R<Void> delete(@PathVariable("id") Long id) {
        sysUserService.delete(id);
        return success();
    }


    /**
     * 导出用户列表
     *
     * @param dto 请求参数
     */
    @PostMapping("/export")
    public void export(HttpServletResponse response, @Validated @RequestBody ExportExcelDto<ListSysUserDto> dto) throws IOException {
        EasyExcelUtil.export(response, dto.getColumns(), sysUserService.export(dto));
    }

}
